package org.csu.mypetstore.controller;

import org.csu.mypetstore.domain.Item;
import org.csu.mypetstore.domain.Journal;
import org.csu.mypetstore.domain.Management;
import org.csu.mypetstore.service.JournalService;
import org.csu.mypetstore.service.ManagementService;
import org.csu.mypetstore.service.PetService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/management")
public class ManagementController {
    @Autowired
    ManagementService managementService;
    @Autowired
    JournalService journalService;
    @Autowired
    PetService petService;

    @PostMapping("/management_confirm")
    public String management_confirm(String username, String password, Model model, HttpSession httpSession, String action, String time) {
        if (username != null && password != null) {
            Management management = managementService.getManagementByUsernameAndPassword(username,password);
            if (management != null) {
                httpSession.setAttribute("management", management);
                httpSession.setAttribute("loginError", null);
                httpSession.setAttribute("username",username);

                Journal journal = new Journal();
                journal.setAction("login");

                time = journal.getTime();
                action=journal.getAction();

                journal.setAction("管理员登录");
                journal.setUsername(username);
                journalService.insertJournal(journal);
                return "management/main";

            } else {
                httpSession.setAttribute("loginError", 1);
                httpSession.setAttribute("management", null);
                return "account/management";
            }
        }
        return "management/main";
    }

    @GetMapping("/viewPet")
    public String viewPet(String categoryId,HttpSession session){
        List<Item> petItemList = petService.getItemList(categoryId);
        session.setAttribute("petItemList",petItemList);
        session.setAttribute("categoryId",categoryId);
        return "management/product";
    }

    @GetMapping("/viewMain")
    public String viewPets(){
        return "management/main";
    }

    @GetMapping("/viewOrder")
    public String viewOrder(){
        return "";
    }

    @GetMapping("/viewAccount")
    public String viewAccount(){
        return "";
    }

    @GetMapping("/unShelve")
    public String unShelve(String itemId,HttpSession session){
        petService.unShelve(itemId);
        String categoryId = (String)session.getAttribute("categoryId");
        List<Item> petItemList = petService.getItemList(categoryId);
        session.setAttribute("petItemList",petItemList);
        session.setAttribute("categoryId",categoryId);
        return "management/product";
    }

    @PostMapping("/upDate")
    public String upDate(String itemId, String name, String productId, BigDecimal listPrice, Integer quantity, HttpSession session){
        petService.upDate(itemId,productId,listPrice,quantity,name);
        String categoryId = (String)session.getAttribute("categoryId");
        List<Item> petItemList = petService.getItemList(categoryId);
        session.setAttribute("petItemList",petItemList);
        session.setAttribute("categoryId",categoryId);
        return "management/product";
    }

    @GetMapping("/viewItem")
    public String viewItem(String itemId,Model model){
        Item item = petService.getItem(itemId);
        model.addAttribute("item",item);
        return "management/item";
    }

    @PostMapping("/upDateItem")
    public String upDateItem(String itemId,String attribute1,Model model){
        petService.upDateItem(itemId,attribute1);
        Item item = petService.getItem(itemId);
        model.addAttribute("item",item);
        return "management/item";
    }
    @GetMapping("/Add")
    public String addNewItem(){
        return "management/newItem";
    }

    @PostMapping("/AddItem")
    public String addNewItem(String categoryId,String name,String description,String productId,String itemId,BigDecimal listPrice,BigDecimal unitCost,String attribute1,int quantity,HttpSession session){
        petService.addNew(categoryId, name, description, productId, itemId, listPrice, unitCost, attribute1, quantity);
        List<Item> petItemList = petService.getItemList(categoryId);
        session.setAttribute("petItemList",petItemList);
        session.setAttribute("categoryId",categoryId);
        return "management/product";
    }
}
